package com.caregiver.watch.business.user.enums;

import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 星期枚举
 */
@Getter
public enum WeekDay {

    SUNDAY(1, 1,"周日"),
    MONDAY(2, 2,"周一"),
    TUESDAY(4, 3,"周二"),
    WEDNESDAY(8, 4,"周三"),
    THURSDAY(16, 5,"周四"),
    FRIDAY(32, 6,"周五"),
    SATURDAY(64, 7,"周六");

    private final int index;
    private final int value;
    private final String description;

    WeekDay(int value, int index, String description) {
        this.value = value;
        this.index = index;
        this.description = description;
    }

    /**
     * 根据位值获取星期枚举
     * @param value 位值
     * @return 星期枚举
     */
    public static WeekDay fromValue(int value) {
        for (WeekDay day : WeekDay.values()) {
            if (day.value == value) {
                return day;
            }
        }
        throw new IllegalArgumentException("Invalid week day value: " + value);
    }

    /**
     * 根据值获取星期枚举
     *
     * @param localDate 时间
     * @return 对应的星期枚举
     */
    public static WeekDay parse(LocalDate localDate) {
        // 获取星期值
        int value = localDate.getDayOfWeek().getValue();
        for (WeekDay day : WeekDay.values()) {
            if (day.index == value) {
                return day;
            }
        }
        throw new IllegalArgumentException("无效的星期值: " + value);
    }

    /**
     * 根据值获取星期枚举
     *
     * @param localDateTime 时间
     * @return 对应的星期枚举
     */
    public static WeekDay parse(LocalDateTime localDateTime) {
        // 获取星期值
        int value = localDateTime.getDayOfWeek().getValue();
        for (WeekDay day : WeekDay.values()) {
            if (day.index == value) {
                return day;
            }
        }
        throw new IllegalArgumentException("无效的星期值: " + value);
    }
}
